package com.company.cloud.tag.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseReturnEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.tag.modules.api.response.Label;
import com.company.cloud.tag.modules.api.response.LabelType;
import com.company.cloud.tag.modules.api.service.LabelRemoteService;
import com.company.cloud.tag.modules.cache.models.LabelModel;
import com.company.cloud.tag.modules.cache.models.LabelTypeModel;
import com.company.cloud.tag.modules.cache.service.LabelCacheService;

@RestController
@Service
public class LabelRemoteServiceImpl implements LabelRemoteService {

	@Autowired
	private LabelCacheService labelCacheService;

	private Label convertLabel(LabelModel entity) {
		Label data = new Label();
		data.setLabelId(entity.getLabelId());
		data.setLabelName(entity.getLabelName());
		data.setLabelTypeNo(entity.getLabelTypeNo());
		data.setLabelTypeName(entity.getLabelTypeName());
		return data;
	}

	private LabelType convertLabelType(LabelTypeModel entity) {
		LabelType data = new LabelType();
		data.setTypeNo(entity.getTypeNo());
		data.setTypeName(entity.getTypeName());
		data.setThemeNo(entity.getThemeNo());
		data.setThemeName(entity.getThemeName());
		return data;
	}

	@Override
	public BaseClientResult<List<Label>> getLabelListByIds(Set<String> ids) {
		try {
			List<Label> result = new ArrayList<Label>();
			if (CollectionUtils.isEmpty(ids)) {
				return BaseClientResult.success(result);
			}
			List<LabelModel> labelList = labelCacheService.getLabelListByCache(ids);
			if (!CollectionUtils.isEmpty(labelList)) {
				labelList.forEach(item -> {
					result.add(this.convertLabel(item));
				});
			}
			return BaseClientResult.success(result);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	@Override
	public BaseClientResult<List<Label>> getLabelListByIds(String ids) {
		try {
			List<Label> result = new ArrayList<Label>();
			if (StringUtils.isEmpty(ids)) {
				return BaseClientResult.success(result);
			}
			Set<String> labelIds = StrSplitUtils.parse2set(ids, String.class);
			return this.getLabelListByIds(labelIds);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	@Override
	public BaseClientResult<Map<String, Label>> getLabelMapByIds(Set<String> ids) {
		try {
			Map<String, Label> result = new HashMap<String, Label>();
			BaseClientResult<List<Label>> labelList = this.getLabelListByIds(ids);
			if (!BaseReturnEnum.SUCCESS.getCode().equals(labelList.getCode())) {
				return BaseClientResult.error(labelList.getCode(), labelList.getMsg());
			}
			if (!CollectionUtils.isEmpty(labelList.getData())) {
				labelList.getData().forEach(item -> {
					result.put(item.getLabelId(), item);
				});
			}
			return BaseClientResult.success(result);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	@Override
	public BaseClientResult<Map<String, Label>> getLabelMapByIds(String ids) {
		try {
			Map<String, Label> result = new HashMap<String, Label>();
			if (StringUtils.isEmpty(ids)) {
				return BaseClientResult.success(result);
			}
			Set<String> labelIds = StrSplitUtils.parse2set(ids, String.class);
			return this.getLabelMapByIds(labelIds);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	@Override
	public BaseClientResult<List<LabelType>> getLabelTypeListByIds(Set<String> ids) {
		try {
			List<LabelType> result = new ArrayList<LabelType>();
			if (CollectionUtils.isEmpty(ids)) {
				return BaseClientResult.success(result);
			}
			List<LabelTypeModel> labelList = labelCacheService.getLabelTypeListByCache(ids);
			if (!CollectionUtils.isEmpty(labelList)) {
				labelList.forEach(item -> {
					result.add(this.convertLabelType(item));
				});
			}
			return BaseClientResult.success(result);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	@Override
	public BaseClientResult<List<LabelType>> getLabelTypeListByIds(String ids) {
		try {
			List<LabelType> result = new ArrayList<LabelType>();
			if (StringUtils.isEmpty(ids)) {
				return BaseClientResult.success(result);
			}
			Set<String> labelIds = StrSplitUtils.parse2set(ids, String.class);
			return this.getLabelTypeListByIds(labelIds);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	@Override
	public BaseClientResult<Map<String, LabelType>> getLabelTypeMapByIds(Set<String> ids) {
		try {
			Map<String, LabelType> result = new HashMap<String, LabelType>();
			BaseClientResult<List<LabelType>> labelTypeList = this.getLabelTypeListByIds(ids);
			if (!BaseReturnEnum.SUCCESS.getCode().equals(labelTypeList.getCode())) {
				return BaseClientResult.error(labelTypeList.getCode(), labelTypeList.getMsg());
			}
			if (!CollectionUtils.isEmpty(labelTypeList.getData())) {
				labelTypeList.getData().forEach(item -> {
					result.put(item.getTypeNo(), item);
				});
			}
			return BaseClientResult.success(result);

		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	@Override
	public BaseClientResult<Map<String, LabelType>> getLabelTypeMapByIds(String ids) {
		try {
			Map<String, LabelType> result = new HashMap<String, LabelType>();
			if (StringUtils.isEmpty(ids)) {
				return BaseClientResult.success(result);
			}
			Set<String> labelIds = StrSplitUtils.parse2set(ids, String.class);
			return this.getLabelTypeMapByIds(labelIds);

		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

	@Override
	public BaseClientResult<List<Label>> test(Set<String> ids) {
		try {
			List<Label> data=new ArrayList<Label>();
			ids.forEach(id->{
				System.out.println("id = "+id);
				Label label=new Label();
				label.setLabelId(id);
				label.setLabelName(id+"_"+new Date().getTime());
				data.add(label);
			});
			return BaseClientResult.success(data);
		} catch (Exception e) {
			return BaseClientResult.error(e);
		}
	}

}
